/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesqUsuario.java
 *
 * Created on 21/01/2010, 17:50:55
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author danilo
 */
public class pesqUsuario extends javax.swing.JInternalFrame {
       static cadastroAnim frameUsuario;


    public void setUsuario(cadastroAnim fUsua){
       frameUsuario = fUsua;
    }

    /** Creates new form pesqUsuario */
    public pesqUsuario() {
        initComponents();

         tfConsulta.requestFocusInWindow();
        tfConsulta.selectAll();

        DefaultTableModel modelo = (DefaultTableModel) tbUsuario.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String consulta = "SELECT PCOD, PNOM FROM CPARC.CAD_PROP ORDER BY PCOD ASC";
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("PCOD")), rs.getString("PNOM")});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbUsuario = new javax.swing.JTable();
        btConsulta = new javax.swing.JButton();
        tfConsulta = new javax.swing.JTextField();

        tbUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome do Proprietário"
            }
        ));
        tbUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbUsuarioKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbUsuario);

        btConsulta.setText("Consultar");
        btConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultaActionPerformed(evt);
            }
        });

        tfConsulta.setText("Digite o Código o nome do Usuário");
        tfConsulta.setToolTipText("Pesquisa de Animais");
        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(36, Short.MAX_VALUE)
                        .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btConsulta)
                        .addGap(27, 27, 27))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsulta))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbUsuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbUsuarioKeyPressed

        if(evt.getKeyCode() == 10 ){

            this.dispose();
            frameUsuario.requestFocusInWindow();
            frameUsuario.usuario((String) tbUsuario.getValueAt(tbUsuario.getSelectedRow(), 0), (String) tbUsuario.getValueAt(tbUsuario.getSelectedRow(), 1));
        }
}//GEN-LAST:event_tbUsuarioKeyPressed

    private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultaActionPerformed

        DefaultTableModel modelo = (DefaultTableModel) tbUsuario.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String pcod = tfConsulta.getText();
        String consulta = null;
        
        if(!pcod.isEmpty()){
            consulta = "SELECT PCOD, PNOM FROM CPARC.CAD_PROP WHERE PNOM LIKE '%" + pcod + "%'";
        }else if (pcod.isEmpty() || pcod.equals("Digite o código do Proprietário")){
            consulta = "SELECT PCOD, PNOM FROM CPARC.CAD_PROP ORDER BY PCOD ASC";
            tfConsulta.requestFocusInWindow();
            tfConsulta.selectAll();
        }
        System.out.println(consulta);
        int i=0;
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("PCOD")), rs.getString("PNOM")});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }


        tbUsuario.requestFocusInWindow();
}//GEN-LAST:event_btConsultaActionPerformed

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed

        if(evt.getKeyCode() == 10){

            btConsulta.doClick();
        }

        else if(evt.getKeyCode() == 27){

            this.dispose();
            frameUsuario.requestFocusInWindow();
            frameUsuario.escUsuario();
        }
}//GEN-LAST:event_tfConsultaKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btConsulta;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tbUsuario;
    public javax.swing.JTextField tfConsulta;
    // End of variables declaration//GEN-END:variables

}
